package org.mparts.client.ui;

import org.mparts.client.content.Images;
import org.mparts.client.content.Strings;
import org.mparts.client.ui.editor.PartitemEditor;
import org.mparts.client.ui.events.SaveEvent;
import org.mparts.client.utils.Utils;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sencha.gxt.widget.core.client.AutoProgressBar;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.FileUploadField;
import com.sencha.gxt.widget.core.client.form.FormPanel;
import com.sencha.gxt.widget.core.client.form.FormPanel.Encoding;
import com.sencha.gxt.widget.core.client.form.FormPanel.Method;

public class LoadCatalogView implements ILoadCatalogView {

	public interface Binder extends UiBinder<Widget, LoadCatalogView> {
	}
	
	@UiField
	Window dialog;
	
	@UiField
	FormPanel form;
	
	@UiField
	FileUploadField uFile;
	
	@UiField
	AutoProgressBar progressBar;
	
	@UiField
	TextButton upload;

	@UiField
	TextButton cancel;

	private ILoadCatalogPresenter presenter;
	
	@Inject
	public LoadCatalogView(Strings strings, Images images, Binder uiBinder) {
		dialog = (Window) uiBinder.createAndBindUi(this);
		dialog.setHeadingText(dialog.getText() + " - " + Utils.getCityName());
		form.setMethod(Method.POST);
		form.setEncoding(Encoding.MULTIPART);
		form.setAction(GWT.getModuleName() + "/upload");
		progressBar.updateText(strings.waitDesc());
	}

	@Override
	public void setPresenter(ILoadCatalogPresenter presenter) {
		this.presenter = presenter;

	}

	@Override
	public Window getDialog() {
		return dialog;
	}
	
	@UiHandler("upload")
	public void onSave(SelectEvent event) {
		if (form.isValid()) {
			presenter.process();
		}

	}

	@UiHandler("cancel")
	public void onCancel(SelectEvent event) {
		dialog.hide();
	}

	@Override
	public FormPanel getForm() {
		return form;
	}

	@Override
	public void showProgress(boolean show) {
		if (show) {
			uFile.getParent().setVisible(false);
			upload.setVisible(false);
			cancel.setVisible(false);
			progressBar.setVisible(true);
			progressBar.auto();
		} else {
			progressBar.reset();
			progressBar.setVisible(false);
			uFile.getParent().setVisible(true);
			upload.setVisible(true);
			cancel.setVisible(true);
		}
	}


}
